﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using personalplaner.common.mvvm;
using System.Windows;
using personalplaner.common.mvvm.commands;
using System.Windows.Input;
using personalplaner.servicemodel.dataaccess;
using personalplaner.business.stamm;
using System.Collections.ObjectModel;
using personalplaner.gui.stamm.viewmodels.models;
using personalplaner.common;
using personalplaner.gui.common;
using personalplaner.business;

namespace personalplaner.gui.stamm.viewmodels
{
    public class SetPersonAnstellungViewModel : BaseViewModel
    {
        public SetPersonAnstellungViewModel(DependencyObject parent)
            : base(parent)
        {
            Model.Von = DateTime.Now;
            Model.Bis = DateTime.Now.AddYears(20);
        }

		public override void Loaded()
		{
			var mw = GetService<IMainWindowDisplayContainer>();
			mw.IsWaitVisible = false;
		}

        #region Commands

        ICommand _okcommand;
        public ICommand OkCommand
        {
            get
            {
                if (_okcommand == null)
                    _okcommand = new RelayCommand(OnOkCommand);
                return _okcommand;
            }
        }

        public void OnOkCommand(object parameter)
        {
			using (new WaitCursor())
			{
				if (Model.SelectedAnstellung == null)
				{
					MessageBox.Show("Keine Anstellung ausgewählt.", "", MessageBoxButton.OK);
					//comboAnstellung.Focus();
					return;
				}

				if (!Model.IsValid)
				{
					Model.RaiseMoveFocus(Model.Error);
					return;
				}

				if (Model.Von > Model.Bis)
				{
					MessageBox.Show("Datum Von ist grösser als Datum Bis.", "", MessageBoxButton.OK);
					//bis.Focus();
					return;
				}

				var da = GetService<IAnstellungDataAccess>();
				da.SetPersonAnstellung(PrsID, Model.SelectedAnstellung.AnstellungID, Model.Von, Model.Bis);

				var wnd = Parent as Window;
				if (wnd == null)
					throw new Exception();

				wnd.DialogResult = true;
				wnd.Close();
			}
        }

        ICommand _cancelcommand;
        public ICommand CancelCommand
        {
            get
            {
                if (_cancelcommand == null)
                    _cancelcommand = new RelayCommand(OnCancelCommand);
                return _cancelcommand;
            }
        }

        public void OnCancelCommand(object parameter)
        {
            var wnd = Parent as Window;
            if (wnd == null)
                throw new Exception();

            wnd.DialogResult = false;
            wnd.Close();
        }

        #endregion

        public override void Unload()
        {
            // use mediator to notify all suscribed methods that a person has to be updated
            Mediator.NotifyColleagues<int>("UpdatePersonAnstellung", PrsID);
        }

        #region Properties

        int _prsid;
        public int PrsID
        {
            get
            {
                return _prsid;
            }
            set
            {
                _prsid = value;
                //CWA: set person here
                RaisePropertyChanged("PrsID");
            }
        }

        SetPersonAnstellungModel _model;
        public SetPersonAnstellungModel Model
        {
            get
            {
                if (_model == null)
                    _model = new SetPersonAnstellungModel();
                return _model;
            }
        }


        IList<Anstellung> _anstellungen;
        public IList<Anstellung> Anstellungen
        {
            get
            {
                if (_anstellungen == null)
                {
                    var da = GetService<IAnstellungDataAccess>();
                    _anstellungen = new ObservableCollection<Anstellung>(da.GetAnstellungen());
                }
                return _anstellungen;
            }
        }

        #endregion
    }
}
